<template>
	<view class="mainpadding2">
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">領取管道</view>
			<u-radio-group v-model="type" placement="row" circle>
				<u-radio activeColor="#ff8505" :customStyle="{marginRight: '30rpx'}" label="自動領取" name="1"></u-radio>
				<u-radio activeColor="#ff8505" label="排隊領取" name="2"></u-radio>
			</u-radio-group>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">優惠方式</view>
			<u-radio-group v-model="data_type" placement="row" circle>
				<u-radio activeColor="#ff8505" :customStyle="{marginRight: '30rpx'}" label="折價" name="1"></u-radio>
				<u-radio activeColor="#ff8505" label="贈品" name="2"></u-radio>
			</u-radio-group>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">使用方式</view>
			<!-- 1=單張,2=多張 -->
			<u-radio-group v-model="use_type" placement="row" circle>
				<u-radio activeColor="#ff8505" :customStyle="{marginRight: '30rpx'}" label="單張" name="1"></u-radio>
				<u-radio activeColor="#ff8505" label="多張" name="2"></u-radio>
			</u-radio-group>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">優惠券名稱</view>
			<input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入優惠券名" v-model="name">
		</view>
		<view class="ffffff mainpadding_top radius">
			<view class="hei_text bold titletext">分享文字管理</view>
			<u--textarea v-model="share_tilte" class="margin_top" placeholder="" maxlength="-1" autoHeight ></u--textarea>
			<!-- <input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入分享文字" v-model="share_tilte"> -->
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  v-if="data_type==1 || data_type==3">
			<view class="hei_text bold titletext">抵扣金額</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入抵扣金額" v-model="dk_money">
		</view>
		<view class="flexbetween ffffff mainpadding_top radius" v-if="data_type==2">
			<view class="hei_text bold titletext">贈品圖片</view>
			<view class="flexleft">
				<view class="positionr margin_right1" v-for="(item,index) in image_text" :key="index">
					<image class="listleftimg radius" :src="item" mode=""></image>
					<view class="deleteimg" @click="removeImg(image,image_text,index)"></view>
				</view>
				<image class="listleftimg radius" src="@/static/image/system/upimg.png" mode=""
					v-if="image_text.length<1" @click="uploadImg(image,image_text,1)"></image>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">最低使用金額</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入最低使用金額" v-model="di_money">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">優惠券張數</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入優惠券張數" v-model="num">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">允許領取數量</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入允許領取數量" v-model="allow_num">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">每次可使用數量</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入每次可使用數量" v-model="use_num">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">隨機領取</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="get_type" @change="changesj" activeColor="#ffb800"></u-switch>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius" @click="selectrole()">
			<view class="hei_text bold titletext">可用分店</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" disabled="" style="text-align: right;" placeholder="請選擇可用分店" :value="rallshop.length?'已選擇':''">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="mainpadding huang_text titletext bold">領取時間</view>
		<view class="ffffff mainpadding flexbetween radius"  @click="messageTime.timeShow=true;timevalue=2">
			<view class="hei_text bold titletext">開始</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="get_starttime" placeholder="請選擇開始時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=3">
			<view class="hei_text bold titletext">結束</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="get_endtime" placeholder="請選擇結束時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="mainpadding huang_text titletext bold">使用時間</view>
		<view class="ffffff mainpadding flexbetween radius"  @click="messageTime.timeShow=true;timevalue=0">
			<view class="hei_text bold titletext">開始</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="starttime" placeholder="請選擇開始時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=1">
			<view class="hei_text bold titletext">結束</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="endtime" placeholder="請選擇結束時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		 <timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<view class="submitbtn margin_top" v-if="!listid" @click="submit">確認</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name:"",
				dk_money:"",
				share_tilte:"",
				di_money:"",
				num:"",
				rallshop:[],
				starttime:"",
				endtime:"",
				get_starttime:"",
				get_endtime:"",
				messageTime:{
					timeShow:false,
					title:"選擇時間",
					mode:"datetime",//date日期，datetime年月日时分秒，time時間，year-month年月
				},
				timevalue:0,//0使用開始，1使用结束，2領取開始，3領取結束
				allow_num:"",
				use_num:1,
				listid:"",
				type:"1",
				starttime_text:"",
				endtime_text:"",
				get_starttime_text:'',
				get_endtime_text:'',
				get_type:false,
				data_type:"1",
				use_type:"1",
				image_text: [],
				image: [],
				issubmit:false
			}
		},
		onLoad(options) {
			if(options.id){
				this.listid = options.id
				uni.setNavigationBarTitle({
					title:"優惠券詳情"
				})
				this.getDetail()
			}
			uni.$on("checkfd",data=>{
				this.rallshop = data
			})
		},
		onUnload() {
			uni.$off("checkfd")
		},
		methods: {
			removeImg(arr, arr_text, index) {
				arr.splice(index, 1)
				arr_text.splice(index, 1)
			},
			getDetail(){
				uni.showLoading({
					mask:true,
					title:"請稍後"
				})
				this.http.request("/api/shop/ralldeail", 'POST', {
					id:this.listid
				}).then(res=>{
					uni.hideLoading()
					let data = res.data
					this.name=data.name
					this.dk_money=data.dk_money
					this.share_tilte = data.share_tilte
					this.di_money=data.di_money
					let arr = []
					data.rallshop.forEach(item=>{
						arr.push({
							id:item.id,
							num:item.num,
							shop_id:item.shop_id,
						})
					})
					this.rallshop = arr
					this.num=data.num
					this.get_type=data.get_type==1?true:false
					this.type = data.type.toString()
					this.data_type = data.data_type.toString()
					this.use_type = data.use_type.toString()
					this.image = data.image?[data.image]:[]
					this.image_text = data.image_text?[data.image_text]:[]
					
					// this.fenshop_id=data.fenshop_id
					this.allow_num = data.allow_num
					this.use_num = data.use_num
					this.starttime=this.http.timereturn(data.starttime *1000)
					// this.starttime_text=this.http.returnDate(data.starttime *1000)
					this.endtime=this.http.timereturn(data.endtime *1000)
					// this.endtime_text=this.http.returnDate(data.endtime *1000)
					
					
					this.get_starttime=this.http.timereturn(data.get_starttime *1000)
					this.get_endtime=this.http.timereturn(data.get_endtime *1000)
				})
			},
			changesj(){
				this.rallshop = []
			},
			selectrole(){
				let data = {
					type:1,
					rallshop:this.rallshop,
					get_type:this.get_type
				}
				uni.navigateTo({
					url:"/pages/workbench/fendianlb?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
			timeConfirm(e){
				if(this.timevalue == 0){
					this.starttime = e
					this.starttime_text = e
				}
				if(this.timevalue == 1){
					this.endtime = e
					this.endtime_text = e
				}
				if(this.timevalue == 2){
					this.get_starttime = e
					this.get_starttime_text = e
				}
				if(this.timevalue == 3){
					this.get_endtime = e
					this.get_endtime_text = e
				}
				console.log(this.get_endtime);
			},
			async submit(){
				if(this.issubmit) return
				if(this.name==""){
					this.http.toast("請輸入優惠券名稱")
					return false
				}
				if(this.dk_money=="" && this.data_type==1){
					this.http.toast("請輸入抵扣金額")
					return false
				}
				if(this.image.length==0 && this.data_type==2){
					this.http.toast("請上傳贈品圖片")
					return false
				}
				if(this.di_money===""){
					this.http.toast("請輸入最低使用金額")
					return false
				}
				// if(Number(this.dk_money)>Number(this.di_money)){
				// 	this.http.toast("抵扣金额不能大于最低使用金额")
				// 	return false
				// }
				if(this.num==""){
					this.http.toast("請輸入優惠券數量")
					return false
				}
				if(this.allow_num==""){
					this.http.toast("請輸入允許領取數量")
					return false
				}
				if(this.use_num=="" || this.use_num==0){
					this.http.toast("請正確輸入每次可使用數量")
					return false
				}
				if(this.use_type==1 && this.use_num!=1){
					this.http.toast("優惠券不叠加每次可使用數量只能為1")
					return false
				}
				
				// if(!this.rallshop.length){
				// 	this.http.toast("請選擇可用分店")
				// 	return false
				// }
				if(this.starttime==""){
					this.http.toast("請選擇使用開始時間")
					return false
				}
				if(this.endtime==""){
					this.http.toast("請選擇使用結束時間")
					return false
				}
				
				
				if(this.get_starttime==""){
					this.http.toast("請選擇領取開始時間")
					return false
				}
				if(this.get_endtime==""){
					this.http.toast("請選擇領取結束時間")
					return false
				}
				let rallshop = this.rallshop
				let fenshop_arr = []
				rallshop.forEach(item=>{
					if(item.id=="") delete item.id
					fenshop_arr.push(item.shop_id)
				})
				fenshop_arr.push(uni.getStorageSync("shop_id"))
				let datamsg = {
					shop_id:uni.getStorageSync("shop_id"),
					name:this.name,
					dk_money:this.dk_money,
					share_tilte:this.share_tilte,
					di_money:this.di_money,
					num:this.num,
					rallshop:this.rallshop,
					starttime:this.starttime,
					endtime:this.endtime,
					type:this.type,
					allow_num:this.allow_num,
					use_num:this.use_num,
					get_starttime:this.get_starttime,
					get_endtime:this.get_endtime,
					get_type:this.get_type?1:0,
					image:this.image[0],
					data_type:this.data_type,
					use_type:this.use_type,
					fenshop_id:fenshop_arr.join(),
				}
				let url = "/api/shop/addrall"
				if(this.listid){
					url = "/api/shop/editrall"
					this.$set(datamsg,"id",this.listid)
				}
				this.issubmit = true
				uni.showLoading({
					mask:true,
					title:"請稍後"
				})
				let res = await this.http.request(url, 'POST', datamsg)
				uni.hideLoading()
				if(res.code==1){
					this.http.toast(res.msg)
					setTimeout(()=>{
						uni.navigateBack(1)
					},500)
				}else{
					this.issubmit = false
					this.http.toast(res.msg)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
::v-deep.u-radio-group {
		flex: none !important;
	}
</style>